package com.tiger.cloud.controller.web;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.common.token.PasswordHashUtil;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.model.system.SysUser;
import com.tiger.cloud.service.system.ISysUserService;
import com.tiger.cloud.util.DateUtils;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;

@Controller
@RequestMapping(value = "sysUser")
@Api(hidden=true)
public class SysUserController extends BaseController {

	private static final Logger logger=Logger.getLogger(SysUserController.class);
	
	@Autowired
	private ISysUserService sysUserService;
	
	@IgnoreAuth
	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request) {
		return "/system/user/user-list";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "updateUserPasswordGet", method = RequestMethod.GET)
	public String updateUserPasswordGet(HttpServletRequest request,@CurrentUserId Long userId) {
		return "/system/user/user-update-password";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "updateUserGet", method = RequestMethod.GET)
	public String updateUserGet(HttpServletRequest request,@CurrentUserId Long userId) {
		SysUser user=sysUserService.get(String.valueOf(userId));
		request.setAttribute("sysUser",user );
		return "/system/user/user-profile";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "resetPassword", method = RequestMethod.POST)
	@ResponseBody
	public Response resetPassword(HttpServletRequest request,SysUser member){
		member.setPassword("123456");
		return ResponseUtil.ok(sysUserService.updatePassword(member));
	}
	
	@IgnoreAuth
	@RequestMapping(value = "updateGet", method = RequestMethod.GET)
	public String update(HttpServletRequest request,String id) {
		SysUser user=sysUserService.get(id);
		request.setAttribute("sysUser",user );
		return "/system/user/user-update";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "insertGet", method = RequestMethod.GET)
	public String insertGet(HttpServletRequest request,String id) {
		return "/system/user/user-insert";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "info", method = RequestMethod.GET)
	public String info(HttpServletRequest request,String id) {
		request.setAttribute("sysUser", sysUserService.get(id));
		return "/user/user-info";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public Response find(@CurrentUserId Long userId,HttpServletRequest request){
		Map<String, Object> params=getPageMap(request);
		Page<SysUser> datas=sysUserService.page(params);
		return ResponseUtil.ok(datas);
	}
	
	@IgnoreAuth
	@RequestMapping(value = "list")
	@ResponseBody
	public Response list(@CurrentUserId Long userId,HttpServletRequest request){
		Map<String, Object> params=getPageMap(request);
		params.put("status", 0);
		List<SysUser> datas=sysUserService.find(params);
		return ResponseUtil.ok(datas);
	}
	
	@IgnoreAuth
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Response update(@CurrentUserId Long userId,HttpServletRequest request,SysUser sysUser){
		Date date = DateUtils.getSysDate();
		sysUser.setUpdateDate(date);
		sysUser.setUpdateUser(String.valueOf(userId));
		sysUserService.update(sysUser);
		return ResponseUtil.ok();
	}
	
	@IgnoreAuth
	@RequestMapping(value = "updateUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public Response updateUserInfo(@CurrentUserId Long userId,HttpServletRequest request,SysUser sysUser){
		Date date = DateUtils.getSysDate();
		sysUser.setUpdateDate(date);
		sysUser.setUpdateUser(String.valueOf(userId));
		sysUserService.update(sysUser);
		return ResponseUtil.ok();
	}
	
	@IgnoreAuth
	@RequestMapping(value = "updatePassword", method = RequestMethod.POST)
	@ResponseBody
	public Response updatePassword(@CurrentUserId Long userId,HttpServletRequest request,SysUser sysUser,String oldPassword){
		Date date = DateUtils.getSysDate();
		if(!StringUtils.isEmpty(oldPassword)){
			SysUser user=sysUserService.get(String.valueOf(userId));
			try {
				if(PasswordHashUtil.validatePassword(oldPassword, user.getPassword())){
					sysUser.setUpdateDate(date);
					sysUser.setId(String.valueOf(userId));
					sysUser.setUpdateUser(String.valueOf(userId));
					sysUserService.updatePassword(sysUser);
					return ResponseUtil.ok();
				}else {
					return ResponseUtil.error(ErrorCode.USER_PASSWORD_NOT_EQUALS);
				}
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (InvalidKeySpecException e) {
				e.printStackTrace();
			}
		}
		return ResponseUtil.error();
	}
	
	@IgnoreAuth
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Response add(@CurrentUserId Long userId,HttpServletRequest request,SysUser sysUser){
		Date date = DateUtils.getSysDate();
		sysUser.setCreateDate(date);
		sysUser.setUpdateDate(date);
		sysUser.setUpdateUser(String.valueOf(userId));
		sysUser.setCreateUser(String.valueOf(userId));
		sysUser.setPassword("88888888");
		sysUserService.insert(sysUser);
		if(StringUtils.isEmpty(sysUser.getImage())){
			sysUser.setImage("static/img/a1.jpg");
		}
		return ResponseUtil.ok();
	}

	@IgnoreAuth
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Response delete(@CurrentUserId Long userId,HttpServletRequest request){
		HashMap<String, Object> param=getParamMap(request);
		sysUserService.remove(param);
		return ResponseUtil.ok();
	}

}